package net.bsayiner.LcdTester;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

public class ScreenTestActivity extends Activity {

	private ImageView imageView;
	private static final String testImagesPath = "images/";
	private static final String testImagesExtention = ".png";
	private String[] images = { "red", "green", "blue", "white", "black", "main", "blackRedGrad", "blackGreenGrad", "blackBlueGrad", "blackWhiteGrad", "blackRedPercent",
			"blackGreenPercent", "blackBluePercent", "blackWhitePercent", "multiple", "pixelMapping" };
	private int count;
	private AlertDialog.Builder alert;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.screen_test_layout);
		imageView = (ImageView) findViewById(R.id.imgScreenTest);
		alert = new AlertDialog.Builder(this);
		alert.setTitle("Test Info");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		Toast.makeText(this, getResources().getString(R.string.screen_test_start_info), Toast.LENGTH_LONG).show();
		onImgScreenTestClick(null);
	}

	public void onImgScreenTestClick(View view) {
		if (count < images.length) {
			Bitmap testImage;
			try {
				testImage = BitmapFactory.decodeStream(getResources().getAssets().open(testImagesPath + images[count] + testImagesExtention));
				imageView.setImageBitmap(testImage);
			} catch (IOException e) {
				e.printStackTrace();
			}
			count++;
		} else {
			count = 0;
			Toast.makeText(this, getResources().getString(R.string.end_of_test), Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.screen_test_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (count <= 5) {
			alert.setMessage(getResources().getString(R.string.screen_test_main_color_info));
			alert.show();
			return true;
		} else if (count == 6) {
			alert.setMessage(getResources().getString(R.string.screen_test_main_image));
			alert.show();
		} else if (count >= 7 && count <= 15) {
			alert.setMessage(getResources().getString(R.string.screen_test_gradient));
			alert.show();
		} else if (count == 16) {
			alert.setMessage(getResources().getString(R.string.screen_test_pixel_mapping));
			alert.show();
		}
		return super.onOptionsItemSelected(item);
	}
}
